<template>
  <van-row class="panel-grade-score">
    <van-cell-group title="考核评分">
      <van-field required label-width="70px" label="考核部门" input-align="right" readonly placeholder="请选择考核部门" v-model="newAssDept"/>
      <van-field required label-width="70px" label="考核载体" input-align="right" readonly placeholder="请选择考核载体" v-model="carrierText"/>
      <van-field required label-width="70px" label="考核内容" input-align="right" readonly placeholder="请选择考核内容" v-model="contentText" @click="startSelectContentItem" is-link/>
      <van-row v-if="carrier.content_item.id !== ''">
        <van-row class="first-row">{{carrier.content_item.item.substr(carrier.content_item.item.indexOf('。') + 1)}}</van-row>
        <van-row class="split-h last-row"/>
      </van-row>
      <van-field required label-width="70px" label="考核事项" input-align="right" readonly placeholder="点击选择考核事项" @click="startSelectScoreItem" is-link/>
      <van-row v-if="carrier.content_item.score_item.length > 0">
        <van-row
          v-for="(score) in carrier.content_item.score_item"
          :key="score.id"
          class="first-row">
          {{score.id + ' - ' + score.item}}
        </van-row>
        <van-row class="split-h last-row"/>
      </van-row>
      <van-field required label-width="70px" label="考核扣分" input-align="right">
        <template #input>
          <van-stepper theme="round" v-model="score" :decimal-length="2" min="0.00" step="0.01" input-width="70%" button-size="22"/>
        </template>
      </van-field>
<!--      <card-receivers v-if="showReceivers" :parentPath="grade.assessment_dept" v-model="receivers"/>-->
      <van-row class="split-h last-row"/>
      <van-row class='operate-area' gutter="10">
        <van-button type="info" @click="submit">保存评分结果</van-button>
      </van-row>
    </van-cell-group>
    <van-popup
      v-model="showPageContentItemList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-content-item-list v-if="showPageContentItemList" :carrier="carrier" :type="$vo.CarrierType.SUB" v-on:onCancel="showPageContentItemList = false" v-on:onConfirm="endSelectContentItem"/>
    </van-popup>
    <van-popup
      v-model="showPageScoreItemList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-sore-item-list v-if="showPageScoreItemList" :carrier="carrier" :type="$vo.CarrierType.SUB" v-on:onCancel="showPageScoreItemList = false" v-on:onConfirm="endSelectScoreItem"/>
    </van-popup>
    <loading-overlay v-if="isLoading" :show="isLoading"/>
  </van-row>
</template>

<script>
import PageContentItemList from '@/views/common/page/PageContentItemList'
import PageSoreItemList from '@/views/common/page/PageSoreItemList'
// import CardReceivers from '@/views/common/card/CardReceivers'
import { updateProblemInst } from '@/api/assess'
export default {
  name: 'PanelGradeScore',
  props: ['grade', 'assessType'],
  components: { PageContentItemList, PageSoreItemList },
  data () {
    return {
      isLoading: false,
      // showPageAssessDeptList: false,
      // showPageCarrierItemList: false,
      showPageContentItemList: false,
      showPageScoreItemList: false,
      assessDept: this.grade.khbm,
      carrier: JSON.parse(this.grade.check_item) ? JSON.parse(this.grade.check_item) : this.$vo.createCarrier(),
      score: this.grade.score,
      receivers: []
    }
  },
  mounted () {
    this.carrier.content_item = this.carrier.content_item ? this.carrier.content_item : { item: '', id: '' }
    this.carrier.content_item.score_item = this.carrier.content_item.score_item ? this.carrier.content_item.score_item : { item: '', id: '' }
  },
  methods: {
    submit () {
      if (this.carrier.item.trim() === '') {
        this.$dialog.alert({
          message: '请选择考核载体'
        })
        return
      }
      if (this.carrier.content_item.item.trim() === '') {
        this.$dialog.alert({
          message: '请选择考核内容'
        })
        return
      }
      if (this.carrier.content_item.score_item.length === 0) {
        this.$dialog.alert({
          message: '请选择考核事项'
        })
        return
      }
      // if (this.receivers.length < 1) {
      //   this.$dialog.alert({
      //     message: '请选择接收人员'
      //   })
      //   return
      // }
      this.$dialog.confirm({
        message: '确认保存评分结果?'
      }).then(() => {
        this.doUpdateGrade(this.getScoreObj())
      }).catch(() => {
      })
    },
    startSelectContentItem () {
      if (this.carrier.id === '') {
        this.$dialog.alert({
          message: '请先选择考核载体'
        })
      } else {
        this.showPageContentItemList = true
      }
    },
    endSelectContentItem (content) {
      this.carrier.content_item = content
      this.showPageContentItemList = false
    },
    startSelectScoreItem () {
      if (this.carrier.content_item.id === '') {
        this.$dialog.alert({
          message: '请先选择考核内容'
        })
      } else {
        this.showPageScoreItemList = true
      }
    },
    endSelectScoreItem (scoreItems) {
      this.carrier.content_item.score_item = scoreItems
      this.showPageScoreItemList = false
    },
    getScoreObj () {
      let grade = JSON.parse(JSON.stringify(this.grade))
      grade.check_item = JSON.stringify(this.carrier)
      grade.score = this.score
      grade.check_userid = this.$store.state.authInfo.user.userid
      grade.check_username = this.$store.state.authInfo.user.username
      let opContent = this.$vo.createOperate()
      opContent.receivers = this.receivers
      let submitObj = {
        op: this.$vo.OperateType.SAVE,
        opcontent: opContent,
        problem_inst: grade
      }
      return submitObj
    },
    doUpdateGrade (obj) {
      // console.log('submit', obj)
      this.isLoading = true
      updateProblemInst(obj).then(res => {
        this.isLoading = false
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          this.$toast.success('评分结果保存成功')
          this.$emit('onConfirm')
        } else {
          this.$dialog.alert({ message: '评分结果保存失败：' + data[this.$response.MESSAGE] })
        }
      }, errFunc => {
        this.isLoading = false
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    }
  },
  computed: {
    newAssDept: function () {
      return this.grade.khbm.substring(this.grade.khbm.indexOf('/') + 1)
    },
    carrierText: function () {
      if (this.carrier.id !== '') {
        return this.carrier.id + ' - ' + this.carrier.item
      } else {
        return ''
      }
    },
    contentText: function () {
      if (this.carrier.content_item.id !== '') {
        return this.carrier.content_item.id + ' - ' + this.carrier.content_item.item.substr(0, this.carrier.content_item.item.indexOf('。'))
      } else {
        return ''
      }
    },
    showReceivers: function () {
      let ret = true
      if (this.assessType + '' === this.$vo.AssessType.DEPT && this.grade.batch_id + '' === '2') {
        ret = false
      }
      return ret
    }
  }
}
</script>

<style scoped lang="less">
  .panel-grade-score {
    .van-field {
      font-size: 12px;
    }
    .first-row {
      font-size: 12px;
      color: #969799;
      padding: 5px 16px;
    }
    .last-row {
      margin-left: 16px;
    }
    .operate-area {
      padding: 20px 16px;
      .van-button {
        width: 100%;
      }
    }
  }
</style>
